package com.smartscreen.baseinfomanage.controller;

import com.smartscreen.baseinfomanage.entity.Teacher;
import com.smartscreen.baseinfomanage.service.TeacherService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/base")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teachers")
    public ApiResponse<List<Teacher>> getAllTeachers() {
        List<Teacher> teachers = teacherService.getAllTeachers();
        return ApiResponse.success(teachers);
    }

    @GetMapping("/teachers/{teacherId}")
    public ApiResponse<Teacher> getTeacher(@PathVariable String teacherId) {
        Teacher teacher = teacherService.getTeacherById(teacherId);
        if (teacher != null) {
            return ApiResponse.success(teacher);
        } else {
            return ApiResponse.error("教师不存在");
        }
    }

    @PostMapping("/teachers")
    public ApiResponse<Teacher> createTeacher(@RequestBody Teacher teacher) {
        Teacher createdTeacher = teacherService.createTeacher(teacher);
        return ApiResponse.created("创建成功", createdTeacher);
    }

    @PutMapping("/teachers/{teacherId}")
    public ApiResponse<Teacher> updateTeacher(@PathVariable String teacherId, @RequestBody Teacher teacher) {
        teacher.setTeacherId(teacherId);
        Teacher updatedTeacher = teacherService.updateTeacher(teacher);
        return ApiResponse.success("更新成功", updatedTeacher);
    }

    @DeleteMapping("/teachers/{teacherId}")
    public ApiResponse<Void> deleteTeacher(@PathVariable String teacherId) {
        teacherService.deleteTeacher(teacherId);
        return ApiResponse.success("删除成功");
    }

    @PostMapping("/teachers/batch")
    public Map<String, Map<String, Object>> getTeachersBatch(@RequestBody List<String> teacherIds) {
        return teacherService.getTeachersBatchAsMap(teacherIds);
    }

}